/*==============================================================================
FILE NAME: Figure_C5.do
CREATED: 25 July 2025
==============================================================================*/


/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  // Define global paths for replication package
} 
*/

*** AIR FIGURES ***
//First, generate shares of each air incident nature
//Point estimates contain numbers for footnote 18
use "$processed_data/unique_complaints_clean.dta", clear
drop if year < 2005
keep if incident_air == 1
gen COUNT = 1
gen n = 1
collapse (sum) ODOR DUST SMOKE OUTDOOR_BURNING OTHER_AIR COUNT, by(n)
foreach x in ODOR DUST SMOKE OUTDOOR_BURNING OTHER_AIR {
gen `x'_SHARE = `x'/COUNT
replace `x'_SHARE = `x'_SHARE*100
}
graph set window fontface "Times New Roman"
graph bar ODOR_SHARE DUST_SHARE SMOKE_SHARE OUTDOOR_BURNING_SHARE, legend(label(1 "Odor") label(2 "Dust") label(3 "Smoke") label(4 "Outdoor Burning") label(5 "Other") size(large) pos(6) col(5) row(2)) ytitle("Share of Complaints (%)", size(large)) ylab(,labsize(large))
graph export "$figures/Figure_C6_Panel_A.pdf", replace

collapse (sum) ODOR DUST SMOKE OUTDOOR_BURNING OTHER_AIR COUNT, by(n)
foreach x in ODOR DUST SMOKE OUTDOOR_BURNING OTHER_AIR {
    gen `x'_SHARE = `x'/COUNT
    replace `x'_SHARE = `x'_SHARE*100
}
export delimited using "$point_estimates/Point_Estimates_Figure_C6_Panel_A.csv", replace


// Generate shares for air incident concerns
use "$processed_data/unique_complaints_clean.dta", clear
drop if year < 2005
keep if incident_air == 1
gen COUNT = 1
gen n = 1
collapse (sum) HEALTH PROPERTY OIL_GAS_SHALE OTHER COUNT, by(n)
foreach x in HEALTH PROPERTY OIL_GAS_SHALE OTHER{
gen `x'_SHARE = `x'/COUNT
replace `x'_SHARE = `x'_SHARE*100
}
graph set window fontface "Times New Roman"
graph bar HEALTH_SHARE PROPERTY_SHARE OIL_GAS_SHALE_SHARE, legend(label(1 "Health") label(2 "Property") label(3 "Oil/Gas/Shale") size(large) pos(6) col(3)) ytitle("Share of Complaints (%)", size(large)) ylab(,labsize(large))
graph export "$figures/Figure_C6_Panel_D.pdf", replace

collapse (sum) HEALTH PROPERTY OIL_GAS_SHALE OTHER COUNT, by(n)
foreach x in HEALTH PROPERTY OIL_GAS_SHALE OTHER{
    gen `x'_SHARE = `x'/COUNT
    replace `x'_SHARE = `x'_SHARE*100
}
export delimited using "$point_estimates/Point_Estimates_Figure_C6_Panel_D.csv", replace


** WATER FIGURES **
//Generate shares for water incident nature 
use "$processed_data/unique_complaints_clean.dta", clear
drop if year < 2005
keep if incident_water == 1
gen COUNT = 1
gen n = 1
collapse (sum) WASTEWATER WATER_SUPPLY_QUALITY STORMWATER WATER_SUPPLY_SERVICE OTHER_WATER COUNT, by(n)
foreach x in WASTEWATER WATER_SUPPLY_QUALITY STORMWATER WATER_SUPPLY_SERVICE OTHER_WATER {
gen `x'_SHARE = `x'/COUNT
replace `x'_SHARE = `x'_SHARE*100
}
graph set window fontface "Times New Roman"
graph bar WASTEWATER_SHARE WATER_SUPPLY_QUALITY_SHARE STORMWATER_SHARE WATER_SUPPLY_SERVICE_SHARE, legend(label(1 "Wastewater") label(2 "Water Quality") label(3 "Stormwater") label(4 "Water Supply") label(5 "Other") size(large) pos(6) col(5) row(2)) ytitle("Share of Complaints (%)", size(large)) ylab(,labsize(large))
graph export "$figures/Figure_C6_Panel_B.pdf", replace

collapse (sum) WASTEWATER WATER_SUPPLY_QUALITY STORMWATER WATER_SUPPLY_SERVICE OTHER_WATER COUNT, by(n)
foreach x in WASTEWATER WATER_SUPPLY_QUALITY STORMWATER WATER_SUPPLY_SERVICE OTHER_WATER {
    gen `x'_SHARE = `x'/COUNT
    replace `x'_SHARE = `x'_SHARE*100
}
export delimited using "$point_estimates/Point_Estimates_Figure_C6_Panel_B.csv", replace

//Generate shares for water incident complaints
use "$processed_data/unique_complaints_clean.dta", clear
drop if year < 2005
keep if incident_water == 1
gen COUNT = 1
gen n = 1
collapse (sum) HEALTH PROPERTY OIL_GAS_SHALE OTHER COUNT, by(n)
foreach x in HEALTH PROPERTY OIL_GAS_SHALE OTHER {
gen `x'_SHARE = `x'/COUNT
replace `x'_SHARE = `x'_SHARE*100
}
graph set window fontface "Times New Roman"
graph bar HEALTH_SHARE PROPERTY_SHARE OIL_GAS_SHALE_SHARE, legend(label(1 "Health") label(2 "Property") label(3 "Oil/Gas/Shale") size(large) pos(6) col(3)) ytitle("Share of Complaints (%)", size(large)) ylab(,labsize(large))
graph export "$figures/Figure_C6_Panel_E.pdf", replace


collapse (sum) HEALTH PROPERTY OIL_GAS_SHALE OTHER COUNT, by(n)
foreach x in HEALTH PROPERTY OIL_GAS_SHALE OTHER {
    gen `x'_SHARE = `x'/COUNT
    replace `x'_SHARE = `x'_SHARE*100
}
export delimited using "$point_estimates/Point_Estimates_Figure_C6_Panel_E.csv", replace

** WASTE FIGURES **
// Generate shares for waste incident nature

use "$processed_data/unique_complaints_clean.dta", clear
drop if year < 2005
keep if incident_waste == 1
gen COUNT = 1
gen n = 1
collapse (sum) ODOR MUNICIPAL INDUSTRIAL PST OTHER_WASTE COUNT, by(n)
foreach x in ODOR MUNICIPAL INDUSTRIAL PST OTHER_WASTE COUNT {
gen `x'_SHARE = `x'/COUNT
replace `x'_SHARE = `x'_SHARE*100
}
graph set window fontface "Times New Roman"
graph bar MUNICIPAL_SHARE ODOR_SHARE INDUSTRIAL_SHARE PST_SHARE, legend(label(1 "Municipal Waste") label(2 "Odor") label(3 "Industrial Waste") label(4 "Petroleum Storage Tanks") label(5 "Other") size(large) pos(6) col(5) row(2)) ytitle("Share of Complaints (%)", size(large)) ylab(,labsize(large))
graph export "$figures/Figure_C6_Panel_C.pdf", replace

collapse (sum) ODOR MUNICIPAL INDUSTRIAL PST OTHER_WASTE COUNT, by(n)
foreach x in ODOR MUNICIPAL INDUSTRIAL PST OTHER_WASTE COUNT {
    gen `x'_SHARE = `x'/COUNT
    replace `x'_SHARE = `x'_SHARE*100
}
export delimited using "$point_estimates/Point_Estimates_Figure_C6_Panel_C.csv", replace

// Generate shares for waste incident complaints

use "$processed_data/unique_complaints_clean.dta", clear
drop if year < 2005
keep if incident_waste == 1
gen COUNT = 1
gen n = 1
collapse (sum) HEALTH PROPERTY OIL_GAS_SHALE OTHER COUNT, by(n)
foreach x in HEALTH PROPERTY OIL_GAS_SHALE OTHER {
gen `x'_SHARE = `x'/COUNT
replace `x'_SHARE = `x'_SHARE*100
}
graph set window fontface "Times New Roman"
graph bar HEALTH_SHARE PROPERTY_SHARE OIL_GAS_SHALE_SHARE, legend(label(1 "Health") label(2 "Property") label(3 "Oil/Gas/Shale") size(large) pos(6) col(3)) ytitle("Share of Complaints (%)", size(large)) ylab(,labsize(large))
graph export "$figures/Figure_C6_Panel_F.pdf", replace

collapse (sum) HEALTH PROPERTY OIL_GAS_SHALE OTHER COUNT, by(n)
foreach x in HEALTH PROPERTY OIL_GAS_SHALE OTHER {
    gen `x'_SHARE = `x'/COUNT
    replace `x'_SHARE = `x'_SHARE*100
}
export delimited using "$point_estimates/Point_Estimates_Figure_C6_Panel_F.csv", replace

